let _util = require('util/util.js');

let indexService = {
    //上传文件，同时上传表单
    addCatalogue: function (params) {
        _util.uploadFile({
            selector: params.selector,
            url: 'http://localhost:3000/catalogue/addCatalogue',
            // url: _util.getServerUrl() + '/catalogue/addCatalogue/',
            $tableRow: params.$tableRow,
            type: "POST"
        });
    },
    //编辑修改目录，同时上传表单
    editCatalogue: function (params) {
        _util.uploadFile({
            selector: params.selector,
            url: 'http://localhost:3000/uploads',
            // url: _util.getServerUrl() + '/uploads/',
            $tableRow: params.$tableRow
        });
    },
    //加载表格信息
    loadTableInfo: function (params) {
        return new Promise((resolve, reject) => {
            _util.request({
                url: 'http://localhost:3000/getCatalogues',
                // url: 'http://111.231.233.160:8080/catalogue/getCatalogue',
                data: {
                    currentPage: params.currentPage || 1,
                    size: params.size || 10
                },
                success: function (res) {
                    let resData = res.result,
                        content = { data: resData.content };

                    if (!res.status) {
                        let result = Object.assign({}, params, content);
                        resolve(result);
                    } else {
                        alert(res.msg);
                        reject();
                    }

                },
                error: function (err) {
                    reject(err);
                }
            })
        })
    },
    //加载图片
    loadImages: function (params) {
        return new Promise(function (resolve, reject) {
            _util.request({
                url: 'http://localhost:3000/documents',
                // url: _util.getServerUrl() + '/documents/',
                data: params.data,
                success: function (res) {
                    resolve(res);
                },
                error: function (err) {
                    reject(err);
                }
            })
        })
    },
    //获取单条目录信息
    getFileInfo: function (params) {
        return new Promise(function (resolve, reject) {
            _util.request({
                url: 'http://localhost:3000/infos',
                // url: _util.getServerUrl() + '/infos/',
                data: params.data,
                success: function (res) {
                    resolve(res);
                },
                error: function (err) {
                    reject(err);
                }
            })
        })
    },
    //上传 excel zip包
    uploadExcelZip: function (params) {
        return new Promise((resolve, reject) => {
            _util.request({
                url: 'http://localhost:3000/uploads/',
                // url: _util.getServerUrl() + '/uploads/',
                method: 'POST',
                cache: false,
                processData: false,
                contentType: false,
                xhr: function () {
                    let xhr = new window.XMLHttpRequest();
                    xhr.addEventListener('progress', function (evt) {
                        if (evt.lengthComputable) {
                            let percentComplete = evt.loaded / evt.total;
                            percentComplete = parseInt(percentComplete * 100);

                            if (percentComplete === 100) {
                                $('.upload-success').css('display', 'block');
                            }

                            $('.progress .progress-bar').css('width', `${percentComplete}%`)
                            $('.progress .progress-bar .percentage').text(`${percentComplete}%`);
                        }
                    });

                    xhr.addEventListener('loadend', function () {
                        $('.jsCloseModal').addClass('in');
                    })
                    return xhr;
                },
                data: params.formData,
                success: function (res) {
                    resolve(res);
                },
                reject: function (err) {
                    reject(err);
                }
            })
        })
    },
    //加载图片的基本表格信息
    loadCertificatePhotosTable: function (params) {
        return new Promise((resolve, reject) => {
            _util.request({
                url: 'http://localhost:3000/certificatePhotos/',
                // url: 'http://111.231.233.160:8080/catalogue/checkCertificate/',
                data: params.data,
                success: function (res) {
                    if (!res.status) {
                        resolve(res.result);
                    }
                },
                error: function (errMsg) {
                    console.log(errMsg);
                }
            })
        });
    },
    //目录软删除
    addToTrash: function (params) {
        return new Promise((resolve, reject) => {
            _util.request({
                url: 'http://111.231.233.160:8080/catalogue/delCatalogue/',
                method: 'DELETE',
                data: params.data,
                succes: function (res) {
                    if (!res.status) {
                        resolve(res.result)
                    }
                },
                error: function (err) {
                    reject(err);
                }
            })
        })
    },
    uploadPhotos: function (params) {
        return new Promise((resolve, reject) => {
            _util.request({
                url: 'http://localhost:3000/uploads/',
                // url: _util.getServerUrl() + ' /importExcelFile/importImage',                 
                method: 'POST',
                cache: false,
                processData: false,
                contentType: false,
                xhr: function () {
                    let xhr = new window.XMLHttpRequest();
                    xhr.addEventListener('progress', function (evt) {
                        if (evt.lengthComputable) {
                            let percentComplete = evt.loaded / evt.total;
                            percentComplete = parseInt(percentComplete * 100);

                            if (percentComplete === 100) {
                                $('.upload-success').css('display', 'block');
                            }

                            $('.progress .progress-bar').css('width', `${percentComplete}%`)
                            $('.progress .progress-bar .percentage').text(`${percentComplete}%`);
                        }
                    });

                    xhr.addEventListener('loadend', function () {
                        $('.jsCloseModal').addClass('in');
                    })
                    return xhr;
                },
                data: params.formData,
                success: function (res) {
                    if (!res.status) {
                        resolve(res);
                    }
                },
                error: function (errMsg) {
                    console.log(errMsg);
                }
            })
        });
    },
    //关键词搜索
    searchByKeyword: function (params) {
        return new Promise((resolve, reject) => {
            _util.request({
                url: 'http://localhost:3000/uploads',
                // url:params.searchByPostion?_util.getServerUrl() + '/catalogue/SearchByLocation/':_util.getServerUrl() + '/catalogue/searchByNameOrFileNumber/',
                data: params.data,
                success: function (res) {
                    if (!res.status) {
                        resolve(res)
                    }
                },
                error: function (err) {
                    console.log(err);
                    reject(err);
                }
            })
        });
    }
}

module.exports = indexService;